import fetcher from "../apis/Fetcher";
import {NumberIdAndName} from "../types/BasicModels";
import {GoodsSummary, QueryGoodsResponse, SubGoodsResponse} from "../apis/GoodsApi";
import {Page, Pageable} from "../utils/Page";
import {ItemOf} from "../types/EnumArray";
import {GoodsSummaryWithPrice} from "./DirectSaleOrderApi";
import {OffsetDateTime} from "@js-joda/core";

export function createProcurementReturn(marketId: number) {
    return fetcher.post<number>(`procurementSuppliers/${marketId}/returnOrders`)
}

export function getProcurementReturnDetail(id: number) {
    return fetcher.get<ProcurementReturnDetail>(`procurementSuppliers/returnOrders/${id}`, {
        includeItems: true
    })
}

export function getProcurementReturnDetailWithoutItems(id: number) {
    return fetcher.get<ProcurementReturnDetail>(`procurementSuppliers/returnOrders/${id}`)
}

export interface ProcurementReturnDetail {
    id: number
    createdDateTime: OffsetDateTime
    createdBy: NumberIdAndName
    items: Array<ProcurementReturnItem>
    supplier : NumberIdAndName
    submittedDateTime?: OffsetDateTime
    receiptDateTime?: OffsetDateTime
    receiptId?: number
}

export interface ProcurementReturnItem extends QueryGoodsResponse {
    amountInOrder: number
    subGoods?:SubGoodsResponse | undefined
    returnPrice:number
    note?:string
}

export interface AddProcurementReturnItemRequest {
    amount: number
    goodsId: number
    unitId?:number
    subGoodsId?:number
    purchasePrice:number
}

export function addProcurementReturnItem(id: number, addProcurementReturnItemRequest: AddProcurementReturnItemRequest) {
    return fetcher.post(`procurementSuppliers/returnOrders/${id}/items`, {}, addProcurementReturnItemRequest)
}

export function deleteProcurementReturnItemSub(orderId: number, goodsId: number,subGoodsId:number) {
    return fetcher.delete(`procurementSuppliers/returnOrders/${orderId}/items/${goodsId}/sub/${subGoodsId}`)
}

export function deleteProcurementReturnItem(orderId: number, goodsId: number) {
    return fetcher.delete(`procurementSuppliers/returnOrders/${orderId}/items/${goodsId}`)
}

export interface UpdateProcurementReturnItemAmountRequest {
    amount: number
    unitId?: number
}

export function updateProcurementReturnItemAmount(orderId: number, goodsId: number, updateProcurementReturnItemAmountRequest: UpdateProcurementReturnItemAmountRequest) {
    return fetcher.patch(`procurementSuppliers/returnOrders/${orderId}/items/${goodsId}/amount`, {}, updateProcurementReturnItemAmountRequest)
}

export function updateProcurementReturnItemSubAmount(orderId: number, goodsId: number, updateProcurementReturnItemAmountRequest: UpdateProcurementReturnItemAmountRequest,subGoodsId:number) {
    return fetcher.patch(`procurementSuppliers/returnOrders/${orderId}/items/${goodsId}/sub/${subGoodsId}/amount`, {}, updateProcurementReturnItemAmountRequest)
}

export interface QueryProcurementReturnRequest {
    supplierIds: number[]
    startDate?: string
    endDate?: string
    createdByIds: number[]
    status?:ProcurementReturnStatus
}

export function queryProcurementReturns(queryProcurementReturnRequest: QueryProcurementReturnRequest, pageable: Pageable) {
    return fetcher.get<Page<QueryProcurementReturnResponse>>("procurementSuppliers/returnOrders", {...queryProcurementReturnRequest, ...pageable})
}

export interface QueryProcurementReturnResponse {
    id: number
    createdBy: NumberIdAndName
    createdDateTime: OffsetDateTime
    lastModifiedDateTime: OffsetDateTime
    goodsKindAmount: number
    supplier: NumberIdAndName
    status: ProcurementReturnStatus
    receiptId?:number
}

export type ProcurementReturnStatus = keyof typeof procurementReturnStatusMap

export const procurementReturnStatusMap =
    {
        "WAIT_SUBMIT": "待提交",
        "SUBMITTED": "已提交",
        "RECEIPT": "已入库",
    } as const
export const procurementReturnStatuses = Object.keys(procurementReturnStatusMap) as ProcurementReturnStatus[]

export function getAllProcurementReturnCreatedEmployees() {
    return fetcher.get<Array<NumberIdAndName>>("procurementSuppliers/returnOrders/employees", {projection: "summary"})
}

export function submitProcurementReturn(id: number) {
    return fetcher.patch<number>(`procurementSuppliers/returnOrders/${id}/submit`)
}

// export function receiptProcurementReturn(id: number) {
//     return fetcher.post<number>(`procurementSuppliers/returnOrders/${id}/receipt`)
// }

export function findAllGoodsWithHistoryProcurementReturnSupplier(id: number) {
    return fetcher.get<GoodsSummaryWithRememberedPrice[]>(`procurementSuppliers/${id}/goods`, {
        projection: "withHistoryProcurementReturn",
    })
}
export interface GoodsSummaryWithRememberedPrice {
    id: number
    name: string
    barcode: string
    subGoods?:SubGoodsResponse
    price: number
    rememberedPrice?: number
    rememberedUnitId?:number
    rememberedAmount?:number

}


export function updateProcurementReturnItemPrice(orderId: number, goodsId: number, price:number){
    return fetcher.patch(`procurementSuppliers/returnOrders/${orderId}/items/${goodsId}/price`, {}, price)
}

export function updateProcurementReturnItemSubPrice(orderId: number, goodsId: number, price:number, subGoodsId:number){
    return fetcher.patch(`procurementSuppliers/returnOrders/${orderId}/items/${goodsId}/sub/${subGoodsId}/price`, {}, price)
}


export function updateProcurementReturnItemNote(orderId: number, goodsId: number,note:string){
    return fetcher.patch(`procurementSuppliers/returnOrders/${orderId}/items/${goodsId}/note`, {}, note)
}

export function updateProcurementReturnItemSubNote(orderId: number, goodsId: number,note:string,subGoodsId:number){
    return fetcher.patch(`procurementSuppliers/returnOrders/${orderId}/items/${goodsId}/sub/${subGoodsId}/note`, {}, note)
}